<?php

	class Admin_proxy extends Model
	{
		public function __construct()
		{
			parent::__construct();
		}
		
		public function decodePersonPasswords()
		{
			$personPasswordArray = array();
			
			$tmp = $this->db->query('
												SELECT    	p.person_id,
															p.password
												FROM      	person p
												INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
												WHERE       p.enabled = 1
												AND			p.cmb_member = 1		
												AND			((hxr.member_type = ' . $this->db->escape( 'Primary' ) . ') OR (hxr.member_type = ' . $this->db->escape( 'Spouse' ) . '))
												AND			p.password IS NOT NULL
											  ');
			
			
			if($tmp->num_rows() > 0)
			{											  
				foreach ($tmp->result() as $personRow)
				{
					$pp = array();
					array_push($pp, $personRow->person_id);
					array_push($pp, $personRow->password);
					
					array_push($personPasswordArray, $pp);
				}
				
				$numPerson = count($personPasswordArray);
			}
			else
				return 0;

			
			for ($i = 0; $i < $numPerson; $i ++)
			{
				$pp = $personPasswordArray[$i];
				$decpswd = $this->encrypt->decode($pp[1]);
				
				$this->db->query('
									UPDATE			person
									SET				password_nh = ' . $this->db->escape($decpswd) . ',
													date_modified = Now()
									WHERE			person_id = ' . $pp[0]  . '
								 ');
			}
							 
			return $tmp->num_rows();
		}		

		
		//Creates passwords, encodes passwords and creates md5 hash
		//For any missing passwords for "Primary" or "Spouse" members
		public function encodePersonPasswords()
		{
			$personPasswordArray = array();
			
			$tmp = $this->db->query('
												SELECT    	p.person_id,
															IFNULL(p.password_nh,'.$this->db->escape( '' ).') AS password_nh,
															IFNULL(p.password,'.$this->db->escape( '' ).') AS password
												FROM      	person p
												INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
												WHERE       p.enabled = 1
												AND			p.cmb_member = 1		
												AND			((hxr.member_type = ' . $this->db->escape( 'Primary' ) . ') OR (hxr.member_type = ' . $this->db->escape( 'Spouse' ) . '))
												AND			((p.password_nh IS NULL) OR (p.password IS NULL))
											  ');
			
			
			if($tmp->num_rows() > 0)
			{											  
				foreach ($tmp->result() as $personRow)
				{
					$pp = array();
					array_push($pp, $personRow->person_id);
					array_push($pp, $personRow->password_nh);
					array_push($pp, $personRow->password);
					
					array_push($personPasswordArray, $pp);
				}
				
				$numPerson = count($personPasswordArray);
			}
			else
				return 0;

			
			for ($i = 0; $i < $numPerson; $i ++)
			{
				$pp = $personPasswordArray[$i];
				
				//If password doesn't exist create it
				if ($pp[1] == "")
				{
					$pp[1] = $this->encrypt->get_random_password();
					
					$encpswd = $this->encrypt->encode($pp[1]);
					$md5pswd = md5( $pp[1] );

					$this->db->query('
										UPDATE			person
										SET				password = ' . $this->db->escape($encpswd) . ',
														password_nh = ' . $this->db->escape($pp[1]) . ',
														password_md5 = ' . $this->db->escape($md5pswd) . ',
														date_modified = Now()
										WHERE			person_id = ' . $pp[0]  . '
									 ');
				}
				else
				{
					if ($pp[2] == "")
					{
						$encpswd = $this->encrypt->encode($pp[1]);
						$md5pswd = md5( $pp[1] );

						$this->db->query('
											UPDATE			person
											SET				password = ' . $this->db->escape($encpswd) . ',
															password_md5 = ' . $this->db->escape($md5pswd) . ',
															date_modified = Now()
											WHERE			person_id = ' . $pp[0]  . '
										 ');
						}
				}

			}
							 
			return $tmp->num_rows();
		}
	
		//Create MD5 password hash for any entries with password_md5 = NULL
		public function encodePersonPasswordsMD5()
		{
			$personPasswordArray = array();
			
			$tmp = $this->db->query('
												SELECT    	p.person_id,
															IFNULL(p.password_nh,'.$this->db->escape( '' ).') AS password_nh
												FROM      	person p
												INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
												WHERE       p.enabled = 1
												AND			p.cmb_member = 1		
												AND			((hxr.member_type = ' . $this->db->escape( 'Primary' ) . ') OR (hxr.member_type = ' . $this->db->escape( 'Spouse' ) . '))
												AND			p.password_nh IS NOT NULL
                        						AND      	p.password_md5 IS NULL
                        						GROUP BY person_id;
											  ');
			
			
			if($tmp->num_rows() > 0)
			{											  
				foreach ($tmp->result() as $personRow)
				{
					$pp = array();
					array_push($pp, $personRow->person_id);
					array_push($pp, $personRow->password_nh);
					
					array_push($personPasswordArray, $pp);
				}
				
				$numPerson = count($personPasswordArray);
			}
			else
				return 0;

			
			for ($i = 0; $i < $numPerson; $i ++)
			{
				$pp = $personPasswordArray[$i];
				
				//If password doesn't exist create it
				if ($pp[1] != "")
				{			
					$md5pswd = md5( $pp[1] );
				
					$this->db->query('
									UPDATE			person
									SET				password_md5 = ' . $this->db->escape($md5pswd) . ',
													date_modified = Now()
									WHERE			person_id = ' . $pp[0]  . '
								 ');
				}
			}
							 
			return $tmp->num_rows();
		}
		
		//Initialize Balavihar attendance for all weeks for current year
		public function initializeBalaviharAttendance( $curyear )
		{
			$weekidarr = array();
						
			$tmp = $this->db->query('
								SELECT bw.bv_week_id
								FROM bv_week bw
								WHERE
									bw.year = ' . $this->db->escape($curyear) . '
								ORDER BY bw.bv_week_id ASC
							');
							 
			$numweeks = $tmp->num_rows();
			
			if( $numweeks > 0)
			{
				$weekidarr = array();
				
				foreach ($tmp->result() as $wkRow)
				{				
					array_push($weekidarr, $wkRow->bv_week_id);
				}
				
				$brhpidarr = array();
							
				//Get all registration entries from previous year, exclude grade 12
				$tmp = $this->db->query('
									SELECT brh.person_id_fk
									FROM bv_reg_history brh
									WHERE
										brh.year = ' . $this->db->escape($curyear-1) . '
										AND brh.grade != ' . $this->db->escape("12") . '
										AND brh.enabled = 1
									ORDER BY brh.bv_reg_history_id ASC
								');
								 
				$numbrh = $tmp->num_rows();
				
				//echo "Number of previous year entries: ".$numbrh."</br>";
				//die;
				
				foreach ($tmp->result() as $brhRow)
				{				
					array_push($brhpidarr, $brhRow->person_id_fk);
				}
				
				//Get all registration entries from current year not in previous year
				$tmp = $this->db->query('
									SELECT brh.person_id_fk
									FROM bv_reg_history brh
									WHERE
										brh.enabled = 1
										AND brh.year = ' . $this->db->escape($curyear) . '
										AND brh.person_id_fk NOT IN
										(SELECT brh1.person_id_fk
										FROM bv_reg_history brh1
										WHERE
											brh1.enabled = 1
											AND brh1.year = ' . $this->db->escape($curyear-1) . ')										
									ORDER BY brh.person_id_fk ASC
								');
								 
				$numbrh = $tmp->num_rows();
				
				//echo "Number of current year entries: ".$numbrh."</br>";
				//die;
				
				foreach ($tmp->result() as $brhRow)
				{				
					array_push($brhpidarr, $brhRow->person_id_fk);
				}
				
				$numbrh = COUNT($brhpidarr);

				$count = 0;
				for( $i = 0; $i < $numweeks; $i++ )
				{
					for( $j = 0; $j < $numbrh; $j++ )
					{

						//Check if this entry is already in table
						$tmp = $this->db->query('
									SELECT bvx.bv_attend_xref_id
									FROM bv_attend_xref bvx
									WHERE
										bvx.bv_week_id_fk = ' . $this->db->escape($weekidarr[$i]) . '
										AND bvx.person_id_fk = ' . $this->db->escape($brhpidarr[$j]) . '
								');

								
						if ($tmp->num_rows() <= 0)
						{
							//Do insert if entry does not exist
							$tmp = $this->db->query('
								INSERT INTO bv_attend_xref (bv_week_id_fk,person_id_fk,year,status)
								VALUES ('.$weekidarr[$i].','.$brhpidarr[$j].','.$curyear.',2)
							');
							
							$count++;
						}
					}
				}
				
				return $count;;
			}
			else
				return -1;
		}
/*
		//Initialize Balavihar attendance for all weeks for current year
		public function initializeBalaviharAttendance( $curyear )
		{
			$weekidarr = array();
						
			$tmp = $this->db->query('
								SELECT bw.bv_week_id
								FROM bv_week bw
								WHERE
									bw.year = ' . $this->db->escape($curyear) . '
								ORDER BY bw.bv_week_id ASC
							');
							 
			$numweeks = $tmp->num_rows();
			
			if( $numweeks > 0)
			{
				$weekidarr = array();
				
				foreach ($tmp->result() as $wkRow)
				{				
					array_push($weekidarr, $wkRow->bv_week_id);
				}
				
				$brhidarr = array();
							
				$tmp = $this->db->query('
									SELECT brh.bv_reg_history_id
									FROM bv_reg_history brh
									WHERE
										brh.year = ' . $this->db->escape($curyear) . '
									ORDER BY brh.bv_reg_history_id ASC
								');
								 
				$numbrh = $tmp->num_rows();
				
				foreach ($tmp->result() as $brhRow)
				{				
					array_push($brhidarr, $brhRow->bv_reg_history_id);
				}
				
				$count = 0;
				for( $i = 0; $i < $numweeks; $i++ )
				{
					for( $j = 0; $j < $numbrh; $j++ )
					{

						//Check if this entry is already in table
						$tmp = $this->db->query('
									SELECT bvx.bv_week_xref_id
									FROM bv_week_xref bvx
									WHERE
										bvx.bv_week_id_fk = ' . $this->db->escape($weekidarr[$i]) . '
										AND bvx.bv_reg_history_id_fk = ' . $this->db->escape($brhidarr[$j]) . '
								');

								
						if ($tmp->num_rows() <= 0)
						{
							//Do insert if entry does not exist
							$tmp = $this->db->query('
								INSERT INTO bv_week_xref (bv_week_id_fk,bv_reg_history_id_fk,status)
								VALUES ('.$weekidarr[$i].','.$brhidarr[$j].',2)
							');
							
							$count++;
						}
					}
				}
				
				return $count;;
			}
			else
				return -1;
		}
*/
		//Initialize Language attendance for all weeks for current year
		public function initializeLanguageAttendance( $curyear )
		{
			$weekidarr = array();
						
			$tmp = $this->db->query('
								SELECT lw.lang_week_id
								FROM lang_week lw
								WHERE
									lw.year = ' . $this->db->escape($curyear) . '
								ORDER BY lw.lang_week_id ASC
							');
							 
			$numweeks = $tmp->num_rows();
			
			if( $numweeks > 0)
			{
				$weekidarr = array();
				
				foreach ($tmp->result() as $wkRow)
				{				
					array_push($weekidarr, $wkRow->lang_week_id);
				}
				
				$lrhidarr = array();
							
				$tmp = $this->db->query('
									SELECT lrh.lang_reg_history_id
									FROM lang_reg_history lrh
									WHERE
										lrh.year = ' . $this->db->escape($curyear) . '
									ORDER BY lrh.lang_reg_history_id ASC
								');
								 
				$numlrh = $tmp->num_rows();
				
				foreach ($tmp->result() as $lrhRow)
				{				
					array_push($lrhidarr, $lrhRow->lang_reg_history_id);
				}
				
				$count = 0;
				for( $i = 0; $i < $numweeks; $i++ )
				{
					for( $j = 0; $j < $numlrh; $j++ )
					{

						//Check if this entry is already in table
						$tmp = $this->db->query('
									SELECT lx.lang_week_xref_id
									FROM lang_week_xref lx
									WHERE
										lx.lang_week_id_fk = ' . $this->db->escape($weekidarr[$i]) . '
										AND lx.lang_reg_history_id_fk = ' . $this->db->escape($lrhidarr[$j]) . '
								');

								
						if ($tmp->num_rows() <= 0)
						{
							//Do insert if entry does not exist
							$tmp = $this->db->query('
								INSERT INTO lang_week_xref (lang_week_id_fk,lang_reg_history_id_fk,status)
								VALUES ('.$weekidarr[$i].','.$lrhidarr[$j].',2)
							');
							
							$count++;
						}
					}
				}
				
				return $count;;
			}
			else
				return -1;
		}

		public function sendRegistrationEmails($curyear)
		{
			$personPasswordArray = array();

			$tmp = $this->db->query('
										SELECT 		p.person_id,
													p.first_name,
													p.email1,
													p.person_name,
													p.password_nh
										FROM person p
										WHERE p.sent_reg_email = 0
										AND p.enabled = 1
										ORDER BY p.person_id ASC;
									');

/*			
			$tmp = $this->db->query('
										SELECT 		p.person_id,
													p.first_name,
													p.email1,
													p.person_name,
													p.password_nh
										FROM person p
										INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
										INNER JOIN household hh ON hh.household_id=hxr.household_id_fk
										WHERE p.cmb_member = 1
										AND p.sent_reg_email = 0
										AND p.first_name <> ' . $this->db->escape( 'Admin' ) . '
										AND hxr.member_type = ' . $this->db->escape( 'Primary' ) . ' 
										AND p.first_name IS NOT NULL
										AND p.email1 IS NOT NULL
										AND p.person_name IS NOT NULL
										AND p.password_nh IS NOT NULL
										AND TRIM(p.first_name) <> ' . $this->db->escape( '' ) . '
										AND TRIM(p.email1) <> ' . $this->db->escape( '' ) . '
										AND TRIM(p.person_name) <> ' . $this->db->escape( '' ) . '
										AND TRIM(p.password_nh) <> ' . $this->db->escape( '' ) . '
										ORDER BY hh.household_id,p.last_name ASC;
									');
*/			
			
			if($tmp->num_rows() > 0)
			{											  
				foreach ($tmp->result() as $personRow)
				{
					$this->sendMemberRegistrationEmail($personRow->first_name, $personRow->email1, $personRow->person_name, $personRow->password_nh, $curyear);
					
					$this->db->query('
									UPDATE			person
									SET				sent_reg_email = 1,
													date_modified = Now()
									WHERE			person_id = ' . $personRow->person_id  . '
								 ');
								 
					sleep(1);
				}

			}
			else
				return 0;
			
			return $tmp->num_rows();
		}
		
		public function sendCWRRegistrationEmails($curyear)
		{
			$personPasswordArray = array();

			$tmp = $this->db->query('
										SELECT 		p.person_id,
													p.first_name,
													p.email1,
													p.person_name,
													p.password_nh
										FROM person p
										WHERE p.sent_cwr_reg_email = 0
										AND p.enabled = 1
										ORDER BY p.person_id ASC;
									');
			
			if($tmp->num_rows() > 0)
			{											  
				foreach ($tmp->result() as $personRow)
				{
					$this->sendMemberCWRRegistrationEmail($personRow->first_name, $personRow->email1, $personRow->person_name, $personRow->password_nh, $curyear);
					
					$this->db->query('
									UPDATE			person
									SET				sent_cwr_reg_email = 1,
													date_modified = Now()
									WHERE			person_id = ' . $personRow->person_id  . '
								 ');
								 
					sleep(1);
				}

			}
			else
				return 0;
			
			return $tmp->num_rows();
		}
		
		public function sendTeacherRosterEmails($curyear)
		{
			$personPasswordArray = array();

			$tmp = $this->db->query('
										SELECT 		p.person_id,
													p.first_name,
													p.email1,
													p.person_name,
													p.password_nh
										FROM person p
										INNER JOIN teacher_group_xref tgx ON tgx.person_id_fk = p.person_id
										WHERE tgx.sent_roster_emails = 0
										AND p.email1 IS NOT NULL
										AND p.person_name IS NOT NULL
										AND p.password_nh IS NOT NULL
										AND p.enabled = 1
										ORDER BY p.person_id ASC;
									');

/*			
			$tmp = $this->db->query('
										SELECT 		p.person_id,
													p.first_name,
													p.email1,
													p.person_name,
													p.password_nh
										FROM person p
										INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
										INNER JOIN household hh ON hh.household_id=hxr.household_id_fk
										WHERE p.cmb_member = 1
										AND p.sent_reg_email = 0
										AND p.first_name <> ' . $this->db->escape( 'Admin' ) . '
										AND hxr.member_type = ' . $this->db->escape( 'Primary' ) . ' 
										AND p.first_name IS NOT NULL
										AND p.email1 IS NOT NULL
										AND p.person_name IS NOT NULL
										AND p.password_nh IS NOT NULL
										AND TRIM(p.first_name) <> ' . $this->db->escape( '' ) . '
										AND TRIM(p.email1) <> ' . $this->db->escape( '' ) . '
										AND TRIM(p.person_name) <> ' . $this->db->escape( '' ) . '
										AND TRIM(p.password_nh) <> ' . $this->db->escape( '' ) . '
										ORDER BY hh.household_id,p.last_name ASC;
									');
*/			
			
			if($tmp->num_rows() > 0)
			{											  
				foreach ($tmp->result() as $personRow)
				{
					$this->sendTeacherRosterEmail($personRow->first_name, $personRow->email1, $personRow->person_name, $personRow->password_nh, $curyear);
					
					$this->db->query('
									UPDATE			teacher_group_xref
									SET				sent_roster_emails = 1
									WHERE			person_id_fk = ' . $personRow->person_id  . '
								 ');
								 
					sleep(1);
				}

			}
			else
				return 0;
			
			return $tmp->num_rows();
		}
		
		public function sendTeacherRosterEmail( $first_name, $email, $username, $password, $curyear )
		{			
			$message = '';
			$message .= '<p>Hari OM ' . $first_name . 'ji,</p>';
			$message .= '<p>To see your class roster for '.$curyear . '-' . ($curyear+1).',  please login to your home page at: </p>';
			$message .= '<p><a href="'.base_url().'/cmb/member/login_home" target="_blank">'.base_url().'/cmb/member/login_home</a></p>';
			$message .= '<p>using the credentials:</p>';
			$message .= '</p>';
			$message .= '<table border="0">';
			$message .= '<tr><td width="150">Username: </td><td>' . $username . '</td></tr>';
			$message .= '<tr><td width="150">Password: </td><td>' . $password . '</td></tr>';
			$message .= '</table>';
			$message .= '</p>';
			$message .= '<p>Click on the <b>Reports</b> button and then click on the <b>My Roster</b> link.</p>';
			$message .= '</p>';
			$message .= '</p>';
			$message .= '<p>Sincerely,</p>';
			$message .= '<p>Registration Team</p>';
			$message .= '<p>Chinmaya Mission Boston<br /></p>';
			
			//echo $message;

			
			$this->load->library('email');
			$this->email->set_newline("\r\n");
			
			$config['protocol'] = $this->config->item('email_smtp_protocol');
			$config['smtp_host'] = $this->config->item('email_smtp_host');
			$config['smtp_user'] = $this->config->item('email_smtp_user');
			$config['smtp_pass'] = $this->config->item('email_smtp_pass');
			$config['smtp_port'] = $this->config->item('email_smtp_port');
			$config['mailtype'] = $this->config->item('email_smtp_mailtype');
	
			$this->email->initialize($config);
	
			$this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
			$this->email->to( $email );
			$this->email->cc('');
			$this->email->bcc('cmbproject@gmail.com');
			
			$this->email->subject('Class Roster for '.$curyear . '-' . ($curyear+1));
			$this->email->message($message);
			
			$this->email->send();

		}
	
		public function sendMemberRegistrationEmail( $first_name, $email, $username, $password, $curyear )
		{			
			$message = '';
			$message .= '<p>Hari OM ' . $first_name . 'ji,</p>';
			$message .= '<p><b>Member Registration for Chinmaya Mission Boston is open for '.$curyear . '-' . ($curyear+1).'.</b></p>';
			$message .= '<p><b><font size="4" color="blue">Several classes are oversusbcribed with students wait-listed.  Your prompt registration & payment online will help complete the registration process.</b></font></p><p><b><font size="4" color="blue">If you chose to pay by check or are planning to register onsite please bring a check book for payment as we will only be accepting checks onsite to speed up the registration process.</font></b></p>';
			$message .= '</p>';
			$message .= '<p>Please login and register at: </p><p>' . base_url() . 'cmb/member/login_register/</p>';
			$message .= '<p>using the credentials:</p>';
			$message .= '<table border="0">';
			$message .= '<tr><td width="150">Username: </td><td>' . $username . '</td></tr>';
			$message .= '<tr><td width="150">Password: </td><td>' . $password . '</td></tr>';
			$message .= '</table>';
			$message .= '</p>';
			$message .= '<p>If you have already registered we apologize for the incorrect communication.  Please let us know when you registered (and the payment method used) and we will update our records.</p>';	
			$message .= '</p>';
			$message .= '<p>Sincerely,</p>';
			$message .= '<p>Registration Team</p>';
			$message .= '<p>Chinmaya Mission Boston<br /></p>';
			
			$this->load->library('email');
			$this->email->set_newline("\r\n");
			
			$config['protocol'] = $this->config->item('email_smtp_protocol');
			$config['smtp_host'] = $this->config->item('email_smtp_host');
			$config['smtp_user'] = $this->config->item('email_smtp_user');
			$config['smtp_pass'] = $this->config->item('email_smtp_pass');
			$config['smtp_port'] = $this->config->item('email_smtp_port');
			$config['mailtype'] = $this->config->item('email_smtp_mailtype');
	
			$this->email->initialize($config);
	
			$this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
			$this->email->to( $email );
			$this->email->cc('');
			$this->email->bcc('cmbproject@gmail.com');
			
			$this->email->subject('Chinmaya Mission Boston Member Registration is Open for '.$curyear . '-' . ($curyear+1));
			$this->email->message($message);
			
			$this->email->send();
		}

        public function sendMemberPaymentReminderEmail( $cwr_registrant_home_value, $curyear, $totfee )
        {
            $this->load->library('encrypt');
            try
            {                       
                $cwr_registrant_home_value = Cwr_registrant_member_homepage_value::cast($cwr_registrant_home_value);
                
                $message = '';
                $message .= '<p>Hari OM ' . $cwr_registrant_home_value->getFirstName() . 'ji,</p>';
                $message .= '<p>You have completed the first part of registration for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1) . '.</p>';
                $message .= '<p>Here is the information we have received:</p>';             
                $message .= '<p>';
                $message .= '<table border="0">';
                $message .= '<tr><td width="150">Username:</td><td>' . $cwr_registrant_home_value->getUserName() . '</td></tr>';
                $message .= '<tr><td width="150">Password:</td><td>' . $this->encrypt->decode($cwr_registrant_home_value->getPassword()) . '</td></tr>';
                $message .= '<tr><td width="250">Name:</td><td>' . $cwr_registrant_home_value->getFirstName() . ' ' . $cwr_registrant_home_value->getLastName() . '</td></tr>';
                $message .= '<tr><td width="250">Address:</td><td>' . $cwr_registrant_home_value->getAddress1() . '</td></tr>';
                $message .= '<tr><td width="250">City:</td><td>' . $cwr_registrant_home_value->getCity() . '</td></tr>';
                $message .= '<tr><td width="250">State:</td><td>' . $cwr_registrant_home_value->getState() . '</td></tr>';
                $message .= '<tr><td width="250">Zip Code:</td><td>' . $cwr_registrant_home_value->getZip() . '</td></tr>';
                $message .= '<tr><td width="250">Email:</td><td>' . $cwr_registrant_home_value->getEmail1() . '</td></tr>';
                $message .= '<tr><td width="250">CMB Registration Donation:</td><td>$' . $cwr_registrant_home_value->getRegAmount() . '</td></tr>';
                $message .= '<tr><td width="250">General Donation:</td><td>$' . $cwr_registrant_home_value->getDonation() . '</td></tr>';
                $message .= '<tr><td width="250"><b>Total:</td><td><u>$' . $totfee . '</u></b></td></tr>';
                $message .= '</table>';
                $message .= '</p>';
                $message .= '<p>Please complete your registration by making a check for <b>$' . $totfee . '</b>, payable to <u>Chinmaya Mission Boston</u>. Mail the check to:</p>';
                $message .= '<table border="0">';
                $message .= '<tr><td width="250"></td><td>ATTN: Member Registration</td></tr>';
                $message .= '<tr><td width="250"></td><td>Chinmaya Mission Boston</td></tr>';
                $message .= '<tr><td width="250"></td><td>1 Union Street</td></tr>';
                $message .= '<tr><td width="250"></td><td>Andover, MA  01810</td></tr>';
                $message .= '</table>';
                $message .= '</p>';
                $message .= '<p>You can also make payment via Paypal or credit card by going to:</p>';
                $message .= '<p><b>http://www.chinmaya-boston.com/donate</b></p>';
                $message .= '<p>Enter the Donation Amount of $'.$totfee.' and enter "2012 CMB Member Registration Donation" in the Comments field.</p>';
                $message .= '<p>You will receive a registration confirmation email as soon as your payment has been approved. We would like to thank you for your support.</p>';
                $message .= '<p>If you have already made a payment we apologize for the incorrect communication.  Please send us the check# or payment transcation id for the payment so that we can update our records.</p>';    
                $message .= '<p>Sincerely,</p>';
                $message .= '<p>Registration Team</p>';
                $message .= '<p>Chinmaya Mission Boston<br/></p>';
                
                //echo $message . "<br/>";
                
                $this->load->library('email');
                $this->email->set_newline("\r\n");
                
                $config['protocol'] = $this->config->item('email_smtp_protocol');
                $config['smtp_host'] = $this->config->item('email_smtp_host');
                $config['smtp_user'] = $this->config->item('email_smtp_user');
                $config['smtp_pass'] = $this->config->item('email_smtp_pass');
                $config['smtp_port'] = $this->config->item('email_smtp_port');
                $config['mailtype'] = $this->config->item('email_smtp_mailtype');

                $this->email->initialize($config);
        
                $this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
                $this->email->to( $cwr_registrant_home_value->getEmail1() );
                $this->email->cc('');
                $this->email->bcc('cmbproject@gmail.com');
                
                $this->email->subject('Donation reminder for Chinmaya Mission Boston registration for ' . $curyear . '-' . ($curyear+1));
                $this->email->message($message);
            
                $this->email->send();
            } catch (Exception $e)
            {
                echo 'Email exception: ',  $e->getMessage(), "\n";
                die;
            }
        }
/*	
		public function sendMemberPaymentReminderEmail( $cwr_registrant_home_value, $curyear, $totfee )
		{
			$this->load->library('encrypt');
			try
			{						
				$cwr_registrant_home_value = Cwr_registrant_member_homepage_value::cast($cwr_registrant_home_value);
				
				$message = '';
				$message .= '<p>Hari OM ' . $cwr_registrant_home_value->getFirstName() . 'ji,</p>';
				$message .= '<p>You have completed the first part of registration for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1) . '.</p>';
				$message .= '<p>Here is the information we have received:</p>';				
				$message .= '<p>';
				$message .= '<table border="0">';
				$message .= '<tr><td width="150">Username:</td><td>' . $cwr_registrant_home_value->getUserName() . '</td></tr>';
				$message .= '<tr><td width="150">Password:</td><td>' . $this->encrypt->decode($cwr_registrant_home_value->getPassword()) . '</td></tr>';
				$message .= '<tr><td width="250">Name:</td><td>' . $cwr_registrant_home_value->getFirstName() . ' ' . $cwr_registrant_home_value->getLastName() . '</td></tr>';
				$message .= '<tr><td width="250">Address:</td><td>' . $cwr_registrant_home_value->getAddress1() . '</td></tr>';
				$message .= '<tr><td width="250">City:</td><td>' . $cwr_registrant_home_value->getCity() . '</td></tr>';
				$message .= '<tr><td width="250">State:</td><td>' . $cwr_registrant_home_value->getState() . '</td></tr>';
				$message .= '<tr><td width="250">Zip Code:</td><td>' . $cwr_registrant_home_value->getZip() . '</td></tr>';
				$message .= '<tr><td width="250">Email:</td><td>' . $cwr_registrant_home_value->getEmail1() . '</td></tr>';
				$message .= '<tr><td width="250">Company name:</td><td>' . $cwr_registrant_home_value->getOrgName() . '</td></tr>';
				$message .= '<tr><td width="250">CMB Registration Fee:</td><td>$' . $cwr_registrant_home_value->getRegAmount() . '</td></tr>';
				$message .= '<tr><td width="250">Balavihar Registration Fee:</td><td>$' . $cwr_registrant_home_value->getBVAmount() . '</td></tr>';
				$message .= '<tr><td width="250">Donation:</td><td>$' . $cwr_registrant_home_value->getDonation() . '</td></tr>';
				$message .= '<tr><td width="250"><b>Total:</td><td><u>$' . $totfee . '</u></b></td></tr>';
				$message .= '</table>';
				$message .= '</p>';
				$message .= '<p>Please complete your registration by making a payment by logging in through the following link:</p>';
				$message .= '<p><b>' . base_url() . 'cmb/member/login_pay_registration/</b></p>';
				$message .= '<p>Note: You can also update your account information by logging in to your home page at:</p>';
				$message .= '<p><b>' . base_url() . 'cmb/member/login_home/</b></p>';
				$message .= '<p>You will receive a registration confirmation email as soon as your payment has been approved. We would like to thank you for your support.</p>';
				$message .= '<p>If you have already registered we apologize for the incorrect communication.  Please let us know when you registered (and the payment method used) and we will update our records.</p>';	
				$message .= '<p>Sincerely,</p>';
				$message .= '<p>Registration Team</p>';
				$message .= '<p>Chinmaya Mission Boston<br/></p>';
				
				//echo $message . "<br/>";
				
				$this->load->library('email');
				$this->email->set_newline("\r\n");
				
				$config['protocol'] = $this->config->item('email_smtp_protocol');
				$config['smtp_host'] = $this->config->item('email_smtp_host');
				$config['smtp_user'] = $this->config->item('email_smtp_user');
				$config['smtp_pass'] = $this->config->item('email_smtp_pass');
				$config['smtp_port'] = $this->config->item('email_smtp_port');
				$config['mailtype'] = $this->config->item('email_smtp_mailtype');

		
				$this->email->initialize($config);
		
				$this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
				$this->email->to( $cwr_registrant_home_value->getEmail1() );
				$this->email->cc('');
				$this->email->bcc('cmbproject@gmail.com');
				
				$this->email->subject('Payment needed for Chinmaya Mission Boston registration for ' . $curyear . '-' . ($curyear+1));
				$this->email->message($message);
			
				$this->email->send();
			} catch (Exception $e)
			{
				echo 'Email exception: ',  $e->getMessage(), "\n";
				die;
			}
		}
*/
		public function sendMemberCheckReminderEmail( $cwr_registrant_home_value, $curyear, $totfee )
		{
			$this->load->library('encrypt');
			try
			{						
				$cwr_registrant_home_value = Cwr_registrant_member_homepage_value::cast($cwr_registrant_home_value);
				
				$message = '';
				$message .= '<p>Hari OM ' . $cwr_registrant_home_value->getFirstName() . 'ji,</p>';
				$message .= '<p>You have completed the first part of registration for Chinmaya Mission Boston for ' . $curyear . '-' . ($curyear+1) . ' and selected payment by check.</p>';
				$message .= '<p><b>Please bring a check made out to <u>Chinmaya Mission Boston</u> for $'.$totfee.' to the upcoming Sunday class to complete payment and confirm your registration.</b></p>';	
				$message .= '<p>Here is the information we have on record:</p>';				
				$message .= '<p>';
				$message .= '<table border="0">';
				$message .= '<tr><td width="150">Username:</td><td>' . $cwr_registrant_home_value->getUserName() . '</td></tr>';
				$message .= '<tr><td width="150">Password:</td><td>' . $this->encrypt->decode($cwr_registrant_home_value->getPassword()) . '</td></tr>';
				$message .= '<tr><td width="250">Name:</td><td>' . $cwr_registrant_home_value->getFirstName() . ' ' . $cwr_registrant_home_value->getLastName() . '</td></tr>';
				$message .= '<tr><td width="250">Address:</td><td>' . $cwr_registrant_home_value->getAddress1() . '</td></tr>';
				$message .= '<tr><td width="250">City:</td><td>' . $cwr_registrant_home_value->getCity() . '</td></tr>';
				$message .= '<tr><td width="250">State:</td><td>' . $cwr_registrant_home_value->getState() . '</td></tr>';
				$message .= '<tr><td width="250">Zip Code:</td><td>' . $cwr_registrant_home_value->getZip() . '</td></tr>';
				$message .= '<tr><td width="250">Email:</td><td>' . $cwr_registrant_home_value->getEmail1() . '</td></tr>';
				$message .= '<tr><td width="250">Company name:</td><td>' . $cwr_registrant_home_value->getOrgName() . '</td></tr>';
				$message .= '<tr><td width="250">CMB Registration Fee:</td><td>$' . $cwr_registrant_home_value->getRegAmount() . '</td></tr>';
				$message .= '<tr><td width="250">Balavihar Registration Fee:</td><td>$' . $cwr_registrant_home_value->getBVAmount() . '</td></tr>';
				$message .= '<tr><td width="250">Donation:</td><td>$' . $cwr_registrant_home_value->getDonation() . '</td></tr>';
				$message .= '<tr><td width="250"><b>Total:</td><td><u>$' . $totfee . '</u></b></td></tr>';
				$message .= '</table>';
				$message .= '</p>';
				$message .= '<p>If you have already sent your check we apologize for the incorrect communication.  Please let us know the check# and we will confirm receipt of the check and correct our records.</p>';	
/*				
				$message .= '<p>Please send your check for <b><u>$' .$totfee. '</u></b> made out to <u>Chinmaya Mission Boston</u> to:</p>';				
				$message .= '<table border="0">';
				$message .= '<tr><td width="250"></td></tr>';
				$message .= '<tr><td width="250">ATTN: Member Registration</td></tr>';
				$message .= '<tr><td width="250">Chinmaya Mission Boston</td></tr>';
				$message .= '<tr><td width="250">1 Union Street</td></tr>';
				$message .= '<tr><td width="250">Andover, MA  01810</td></tr>';
				$message .= '</table>';				
				$message .= '</p>';
*/				
				$message .= '<p>Note: You can update your account information by logging in to your home page at:</p>';
				$message .= '<p><b>' . base_url() . 'cmb/member/login_home/</b></p>';
				$message .= '<p>You will receive a registration confirmation email as soon as your check has been received. We would like to thank you for your support.</p>';
				//$message .= '<p>Note: For registrations not paid <b><u>after July 31<sup>st</sup></u></b> sessions will be assigned according to availability in each class, therefore please try to send your check ASAP.</p>';
				$message .= '<p>Sincerely,</p>';
				$message .= '<p>Registration Team</p>';
				$message .= '<p>Chinmaya Mission Boston<br/></p>';
				
				$this->load->library('email');
				$this->email->set_newline("\r\n");
				
				$config['protocol'] = $this->config->item('email_smtp_protocol');
				$config['smtp_host'] = $this->config->item('email_smtp_host');
				$config['smtp_user'] = $this->config->item('email_smtp_user');
				$config['smtp_pass'] = $this->config->item('email_smtp_pass');
				$config['smtp_port'] = $this->config->item('email_smtp_port');
				$config['mailtype'] = $this->config->item('email_smtp_mailtype');
		
				$this->email->initialize($config);
		
				$this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
				$this->email->to( $cwr_registrant_home_value->getEmail1() );
				$this->email->cc('');
				$this->email->bcc('cmbproject@gmail.com');
				
				$this->email->subject('Payment needed for Chinmaya Mission Boston registration for ' . $curyear . '-' . ($curyear+1));
				$this->email->message($message);
			
				$this->email->send();
				
			} catch (Exception $e)
			{
				echo 'Email exception: ',  $e->getMessage(), "\n";
				die;
			}
		}

		public function sendMemberCWRRegistrationEmail( $first_name, $email, $username, $password, $curyear )
		{			
			$message = '';
			$message .= '<p>Hari OM ' . $first_name . 'ji,</p>';
			$message .= '<p><b>Chinmaya Mission Boston Walk/Run for CORD '.$curyear.' Registration is Open</b></p>';
			$message .= '</p>';
			$message .= '<p>You may register online at: </p><p>http://www.cmbcordwalk.org/</p>';
			$message .= '<p>by clicking on the <u>Register</u> button.  Also, volunteers will be glad to register you at the Community Hall, CMB, Andover on Sunday, 25-September & Sunday, 2-October.</p>';
			$message .= '</p>';
			$message .= '<p>Chinmaya Mission Boston hosts an annual Walk/Run to help raise funds for CORD.  With your help we have raised almost $25,000 for CORD over the past two years.</p>';
			$message .= '</p>';
			$message .= '<p>Chinmaya Organization for Rural Development (CORD) is an internationally-recognized, India-based non-profit organization that helps the rural poor transform their lives through self-driven programs. CORD supports almost 600 villages in Andhra Pradesh, Himachal Pradesh, Orissa and Tamil Nadu, providing direct help to over 50,000 villagers.  More information is available at <a href="http://www.cmbcordwalk.org/index.php/walk/walk_info/learn_more/" target="_blank">About CORD.</a></p>';
			$message .= '</p>';
			$message .= '<p>The event will be held on Sunday, October 8th in a beautiful park (Great Brook Farm State Park in Carlisle, MA) over a flat 5km trail that is easy for young and old alike.  Please join us in supporting CORD and having <b><i><font color = "DarkRed">FUN!.</font></i></b>  There are activities and prizes for children and snacks will be served.</p>';
			$message .= '</p>';
			$message .= '<p>Sincerely,</p>';
			$message .= '<p>Seva Team</p>';
			$message .= '<p>Chinmaya Mission Boston<br /></p>';
			
			$this->load->library('email');
			$this->email->set_newline("\r\n");
			
			$config['protocol'] = $this->config->item('email_smtp_protocol');
			$config['smtp_host'] = $this->config->item('email_smtp_host');
			$config['smtp_user'] = $this->config->item('email_smtp_user');
			$config['smtp_pass'] = $this->config->item('email_smtp_pass');
			$config['smtp_port'] = $this->config->item('email_smtp_port');
			$config['mailtype'] = $this->config->item('email_smtp_mailtype');
	
			$this->email->initialize($config);
	
			$this->email->from($this->config->item('email_smtp_user'), $this->config->item('email_smtp_user_name'));
			$this->email->to( $email );
			$this->email->cc('');
			$this->email->bcc('cmbproject@gmail.com');
			
			$this->email->subject('CMB Walk/Run for CORD '.$curyear . ' Registration is Open');
			$this->email->message($message);
			
			$this->email->send();
		}
	
		//Set "sent_payment_email" = 1
		public function setPaymentEmailSent( $cmbRegHistoryId )
		{
			$this->db->query('
							UPDATE			cmb_reg_history
							SET				sent_payment_email = 1,
											date_modified = Now()
							WHERE			cmb_reg_history_id = ' . $this->db->escape( $cmbRegHistoryId ) . '
						 ');
	
			return 	0;		
		}		

		//Set "sent_reg_confirm_email" = 1
		public function setRegConfirmEmailSent( $cmbRegHistoryId )
		{
			$this->db->query('
							UPDATE			cmb_reg_history
							SET				sent_reg_confirm_email = 1,
											date_modified = Now()
							WHERE			cmb_reg_history_id = ' . $this->db->escape( $cmbRegHistoryId ) . '
						 ');
	
			return 	0;		
		}		

		//Get cmb_reg_history id's for members who have not paid
		public function getNotPaidID( $curyear )
		{
			$regidarr = array();
/*			
			$tmp = $this->db->query('
												SELECT    	crh.cmb_reg_history_id
												FROM      	cmb_reg_history crh
												WHERE       crh.enabled = 1
												AND			crh.year = ' . $this->db->escape( $curyear ) . '		
												AND			crh.fully_paid = 0
												AND			crh.source = ' . $this->db->escape( "unknown" ) . '
												AND			crh.obj_temp_id > 0
												AND			sent_payment_email = 0;
											  ');
*/					
            $tmp = $this->db->query('
                                                SELECT      crh.cmb_reg_history_id
                                                FROM        cmb_reg_history crh
                                                WHERE       crh.enabled = 1
                                                AND         crh.year = ' . $this->db->escape( $curyear ) . '
                                                AND         crh.type = ' . $this->db->escape( "Regular" ) . '
                                                AND         crh.fully_paid = 0
                                                AND         sent_payment_email = 0;
                                              ');
			if($tmp->num_rows() > 0)
			{											  
				foreach ($tmp->result() as $regRow)
				{
					array_push($regidarr, $regRow->cmb_reg_history_id);
				}
			}
	
			return 	$regidarr;		
		}

		//Get cmb_reg_history id's for members who have paid
		public function getPaidID( $curyear )
		{
			$regidarr = array();
			
			$tmp = $this->db->query('
												SELECT    	crh.cmb_reg_history_id
												FROM      	cmb_reg_history crh
												WHERE       crh.enabled = 1
												AND			crh.year = ' . $this->db->escape( $curyear ) . '		
												AND			crh.obj_temp_id = 0
												AND			crh.fully_paid = 1
												AND			crh.payment_status = 1
												AND			sent_reg_confirm_email = 0;
											  ');
					
			if($tmp->num_rows() > 0)
			{											  
				foreach ($tmp->result() as $regRow)
				{
					array_push($regidarr, $regRow->cmb_reg_history_id);
				}
			}
	
			return 	$regidarr;		
		}

		//Get cmb_reg_history id's for members who have registered and specified payment by check, but have to send a check
		public function getNotPaidByCheckID( $curyear )
		{
			$regidarr = array();
			
			$tmp = $this->db->query('
												SELECT    	crh.cmb_reg_history_id
												FROM      	cmb_reg_history crh
												WHERE       crh.enabled = 1
												AND			crh.year = ' . $this->db->escape( $curyear ) . '		
												AND			crh.fully_paid = 0
												AND			crh.source = ' . $this->db->escape( "Offline" ) . '
												AND			crh.payment_type = ' . $this->db->escape( "check" ) . '
												AND			ABS(crh.registration_amount+crh.donation_amount+crh.bv_amount+crh.lang_amount-crh.amount_paid) < 0.01
												AND			crh.obj_temp_id = 0
												AND			sent_payment_email = 0;
											  ');
					
			if($tmp->num_rows() > 0)
			{											  
				foreach ($tmp->result() as $regRow)
				{
					array_push($regidarr, $regRow->cmb_reg_history_id);
				}
			}
	
			return 	$regidarr;		
		}
	}		
?>